package chen.javaee_experiment3.Service.Impl;

import chen.javaee_experiment3.Entity.VirusData;
import chen.javaee_experiment3.Service.VirusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
public class virusServiceImpl implements VirusService {

    public static Resource virusDataResource;
    public static final String url = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";

    @Scheduled(cron = "0 0 1 * * *")
    @Override
    public void afterPropertiesSet(){
        RequestEntity<Void> requestEntity =
                RequestEntity.get(URI.create(url))
                        .headers(httpHeaders -> httpHeaders.add("User-Agent", "Mozilla/5.0"))
                        .build();
        ResponseEntity<Resource> exchange = new RestTemplate().exchange(requestEntity, Resource.class);
        virusDataResource =  exchange.getBody();
        log.info("启动应用程序时获取一次统计数据");
    }

    @Override
    public List<VirusData> analysisCSV(){
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        List<VirusData> virusDataList = new ArrayList<>();
        try {
            inputStream = virusDataResource.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader("Province/State", "Country/Region", "Lat", "Long").parse(bufferedReader);
            Iterator<CSVRecord> iterator = records.iterator();
            iterator.next();
            while (iterator.hasNext()) {
                CSVRecord record = iterator.next();
                VirusData virusData = new VirusData(
                        record.get("Province/State"),
                        record.get("Country/Region"),
                        Integer.parseInt(record.get(record.size() - 1)),
                        Integer.parseInt(record.get(record.size() - 1)) - Integer.parseInt(record.get(record.size() - 2))
                );
                virusDataList.add(virusData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return virusDataList;
    }

}
